package com.qeado.report.service.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qeado.report.dao.ExportUserInfoDao;
import com.qeado.report.model.UserExport;
import com.qeado.report.service.ExportUserInfoService;

@Service
public class ExportUserInfoServiceImpl implements ExportUserInfoService {
	@Autowired
	private ExportUserInfoDao exportUserInfoDao;

	public List<UserExport> userInfoExportSearch(UserExport userExport) {
		return exportUserInfoDao.userInfoExportSearch(userExport);
	}

	public WritableWorkbook userInfoToExcel(UserExport userExport, OutputStream out) {
		List<UserExport> userExportList = exportUserInfoDao.userInfoExportSearch(userExport);
		WritableWorkbook book = null;
		try {
			book = Workbook.createWorkbook(out);
			WritableSheet sheet = book.createSheet("注册用户信息", 0); // 创建Sheet
			//表头标题字体格式
			WritableFont headTitleFont = new WritableFont(WritableFont.createFont("宋体_GB2312"), 10, WritableFont.BOLD, false);
			//表头标题表格样式
			WritableCellFormat headTitleFormat = new WritableCellFormat(headTitleFont);
			headTitleFormat.setBackground(jxl.format.Colour.PALE_BLUE);
			headTitleFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
			headTitleFormat.setWrap(true);//自动换行
			headTitleFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			headTitleFormat.setAlignment(jxl.format.Alignment.CENTRE);
			//内容字体格式
			WritableFont contentFont = new WritableFont(WritableFont.createFont("宋体_GB2312"), 10, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
			//内容表格样式，居中对齐
			WritableCellFormat contentCenterFormat = new WritableCellFormat(contentFont);
			contentCenterFormat.setBackground(jxl.format.Colour.LIGHT_GREEN);
			contentCenterFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
			contentCenterFormat.setWrap(true);//自动换行
			contentCenterFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			contentCenterFormat.setAlignment(jxl.format.Alignment.CENTRE);
			//靠左对齐
			WritableCellFormat contentLeftFormat = new WritableCellFormat(contentFont);
			contentLeftFormat.setBackground(jxl.format.Colour.LIGHT_GREEN);
			contentLeftFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
			contentLeftFormat.setWrap(true);//自动换行
			contentLeftFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			contentLeftFormat.setAlignment(jxl.format.Alignment.LEFT);
			
			sheet.addCell(new Label(0, 0, "用户名", headTitleFormat)); // 标题格式的单元格
			sheet.addCell(new Label(1, 0, "电子邮箱", headTitleFormat));
			sheet.addCell(new Label(2, 0, "手机号码", headTitleFormat));
			sheet.addCell(new Label(3, 0, "固定电话", headTitleFormat));
			sheet.addCell(new Label(4, 0, "性别", headTitleFormat));
			sheet.addCell(new Label(5, 0, "注册日期", headTitleFormat));
			
			sheet.setColumnView(0, 20); // 设置列的宽度
			sheet.setColumnView(1, 30); 
			sheet.setColumnView(2, 20); 
			sheet.setColumnView(3, 20); 
			sheet.setColumnView(4, 10); 
			sheet.setColumnView(5, 20); 
			
			if(userExportList != null) {
				for(int i=0;i<userExportList.size();i++) {
					UserExport model = userExportList.get(i);
					sheet.addCell(new Label(0, i+1, model.getUserName(), contentLeftFormat));
					sheet.addCell(new Label(1, i+1, model.getEmail(), contentLeftFormat));
					sheet.addCell(new Label(2, i+1, model.getMobileNo(), contentLeftFormat));
					sheet.addCell(new Label(3, i+1, model.getTelNo(), contentLeftFormat));
					sheet.addCell(new Label(4, i+1, model.getSex(), contentCenterFormat));
					sheet.addCell(new Label(5, i+1, model.getSignTime(), contentCenterFormat));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
		return book;
	}
}
